<?php
class JobDetailsController extends AppController {

	var $name = 'JobDetails';
	var $components = array('Email');
	
	function beforeFilter(){
		if($this->Session->read('Auth.User')){
			$this->Auth->allow('*');
		}
	}
	
	function admin_export_xls(){
		$this->render('export_xls','export_xls');
		//$this->layout = 'export_xls';
	}
	
	function view($id = NULL){
		$detail = $this->JobDetail->find('first', array('conditions' => array('JobDetail.id' => $id)));
		$this->set('job', $detail);
	}

	function admin_index() {
		$this->set('jobDetails', $this->paginate());
	}

	function admin_add($id = NULL) {
		if (!empty($this->data)) {
			$this->JobDetail->create();
			if ($this->JobDetail->save($this->data)) {
				$this->Session->setFlash(__('The job detail has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The job detail could not be saved. Please, try again.', true));
			}
		}
		if($id != NULL){
			$this->data = $this->JobDetail->read(null, $id);
		}
		$jobProviders = $this->JobDetail->JobProvider->find('list');
		$parents = $this->JobDetail->JobCategory->find('list', array(
																		'fields' => array(
																			'id','parent_id'),
																		'conditions' => array(
																			'parent_id <>' => NULL )));
		$jobCategories = $this->JobDetail->JobCategory->find('list', array(
																		'fields' => array(
																			'id','name'),
																		'conditions' => array(
																			'parent_id <>' => NULL )));
		$this->set(compact('jobProviders', 'jobCategories', 'parents'));
	}

	function admin_delete($id = null) {
		if(($this->RequestHandler->isAjax())) {
			$this->autoRender = false;
			if (!$id) {
				return 'error';
			}
			if ($this->JobDetail->delete($id)) {
				return 'Success';
			}
			return 'error';
		}
	}
	
	function admin_delete_staff($id = null, $company = NULL) {
		if(($this->RequestHandler->isAjax())) {
			$this->autoRender = false;
			if (!$id) {
				return 'error';
			}
			if ($this->JobDetail->Staff->delete($id)) {
				return 'Success';
			}
			return 'error';
		}else{
			if(!$id){
				$this->redirect(array('action' => 'index'));
			}else{
				$detail = $this->JobDetail->Staff->read(null, $id);
				$detailID = $detail['JobDetail'][0]['id'];
				if ($this->JobDetail->Staff->delete($id)) {
					$this->redirect(array('action' => 'staff_view',$detailID));
				}else{
					$this->redirect(array('action' => 'index'));
				}
			}
		}
	}
	
	function admin_staff_view($id = NULL){
		if($id != NULL){
			$staffs = $this->JobDetail->find('all', array(
														'conditions' => array(
																'JobDetail.id' => $id)));
			$this->set('staffs',$staffs);
			$this->set('company', $id);
		}else{
			$this->redirect(array('action' => 'index'));
		}
	}
	
	function admin_staff($id = NULL,$company = NULL){
		if ($id == NULL) {
			$this->redirect(array('action' => 'index'));
		}if($company != NULL){
			$this->set('back',$company);
		}
		$this->set('staff', $this->JobDetail->Staff->read(null, $id));
		$this->loadModel('LanguageSkill');
		$this->loadModel('LanguageLevel');
		$this->loadModel('OaSkill');
		$this->loadModel('SkillLevel');
		$this->set('languages', $this->LanguageSkill->find('list'));
		$this->set('languagesLevel',$this->LanguageLevel->find('list'));
		$this->set('oaSkill', $this->OaSkill->find('list'));
		$this->set('oaLevel', $this->SkillLevel->find('list'));
	}
	
	function generate(){
		$this->layout = 'ajax';
	}
	
	/*function search(){
		if($this->RequestHandler->isAjax()){
			$conditions = array();
			$filteredData = '';
			$searched = false;
			$this->autoRender = false;
			$jobLocations = isset($this->data['JobProvider']['job_location_id'])?$this->data['JobProvider']['job_location_id']:'';
			$minSalary = isset($this->data['JobDetail']['salary'])?$this->data['JobDetail']['salary']:'';
			$categories = isset($this->data['JobCategory']['JobCategory'])?$this->data['JobCategory']['JobCategory']:'';
			unset($this->data);
			$this->data = '';
			if($jobLocations != ''){
				$searched = true;
				$filteredData = '';
				$tempLocation = $this->JobDetail->JobProvider->JobLocation->children($jobLocations,false, array('id'));
				$location[] = $jobLocations;
				foreach($tempLocation as $key => $value):
					$location[] = $value['JobLocation']['id'];
				endforeach;
				unset($tempLocation);
				$providers = $this->JobDetail->JobProvider->find('list', array('conditions' => array(
																						'JobProvider.job_location_id' => $location,
																						 ),
																				'fields' => array('id','id')));
				$filteredData = $this->JobDetail->find('list', array('conditions' => array(
																			'AND' => array(
																				'JobDetail.job_provider_id' => $providers,
																				'JobDetail.end_date >=' => date("Y-m-d")))));
			}
			if($minSalary != ''){
				if(intval($minSalary) == $minSalary){
					$conditions['JobDetail.salary >='] = intval($minSalary);
					if($searched){
						$filteredData = $this->JobDetail->find('list', array('conditions' => array(
																					'AND' => array(
																						'JobDetail.id' => $filteredData,
																						'JobDetail.salary >=' => intval($minSalary),
																						'JobDetail.end_date >=' => date("Y-m-d")))));
					}else{
						$filteredData = $this->JobDetail->find('list', array('conditions' => array(
																					'AND' => array(
																						'JobDetail.salary >=' => intval($minSalary),
																						'JobDetail.end_date >=' => date("Y-m-d")))));
					}
					$searched = true;  
				}
			}
			if($categories != ''){
				$categoryId = $this->JobDetail->JobCategoriesJobDetail->find('list', array('conditions' => array(
																									'JobCategoriesJobDetail.job_category_id' => $categories),
																							
																							'fields' => array('JobCategoriesJobDetail.id', 'JobCategoriesJobDetail.job_detail_id')));
				if($searched){
					$filteredData = $this->JobDetail->find('list', array('conditions' => array(
																					'AND' => array(
																						'JobDetail.id' => $filteredData,
																						'JobDetail.id' => $categoryId,
																						'JobDetail.end_date >=' => date("Y-m-d")))));
				}else{
					$filteredData = $this->JobDetail->find('list', array('conditions' => array(
																				'AND' => array(
																						'JobDetail.id' => $categoryId,
																						'JobDetail.end_date >=' => date("Y-m-d")))));
				}
			}
			if($filteredData == '' || empty($filteredData)){
				$this->set('notFound', true);
			}else{
				$this->set('jobs',$this->JobDetail->find('all', array('conditions' => array(
																						'JobDetail.id' => $filteredData,
																						))));
			}
			$this->render('search_result');
		}else{
			if(!empty($this->data)){
				$locations = $this->JobDetail->JobProvider->JobLocation->children($this->data['JobProvider']['job_location_id']);
				$minSalary = $this->data['JobDetail']['salary'];
				$categories = $this->data['JobCategory']['JobCategory'];
			}
			$jobLocations = $this->JobDetail->JobProvider->JobLocation->find('list', array('conditions' => array(
																							'JobLocation.parent_id' => NULL)));
			$jobCategories = $this->JobDetail->JobCategory->find('list', array(
																			'fields' => array(
																				'id','name'),
																			'conditions' => array(
																				'parent_id <>' => NULL )));
			$this->set('jobLocations',$jobLocations);
			$this->set('jobCategories',$jobCategories);
		}
	}*/
	
	function search($id = NULL, $type = NULL){
		if($id != NULL && $type != NULL){
			if($type == 'category'){
				$this->data['JobDetail']['category_id'] = $id;
			}else if($type == 'location'){
				$this->data['JobProvider']['job_location_id'] = $id;
			}
		}
		if(!empty($this->data)){
			$this->set('criteria',$this->data);
			$conditions = array();
			$filteredData = '';
			$searched = false;
			$jobLocations = isset($this->data['JobProvider']['job_location_id'])?$this->data['JobProvider']['job_location_id']:'';
			$minSalary = isset($this->data['JobDetail']['salary'])?$this->data['JobDetail']['salary']:'';
			$categories = isset($this->data['JobDetail']['category_id'])?$this->data['JobDetail']['category_id']:'';
			unset($this->data);
			$this->data = '';
			if($jobLocations != ''){
				$searched = true;
				$filteredData = '';
				$tempLocation = $this->JobDetail->JobProvider->JobLocation->children($jobLocations,false, array('id'));
				$location[] = $jobLocations;
				foreach($tempLocation as $key => $value):
					$location[] = $value['JobLocation']['id'];
				endforeach;
				unset($tempLocation);
				$providers = $this->JobDetail->JobProvider->find('list', array('conditions' => array(
																						'JobProvider.job_location_id' => $location,
																						 ),
																				'fields' => array('id','id')));
				$filteredData = $this->JobDetail->find('list', array('conditions' => array(
																			'AND' => array(
																				'JobDetail.job_provider_id' => $providers,
																				'JobDetail.end_date >=' => date("Y-m-d")))));
			}
			if($minSalary != ''){
				if(intval($minSalary) == $minSalary){
					$conditions['JobDetail.salary >='] = intval($minSalary);
					if($searched){
						$filteredData = $this->JobDetail->find('list', array('conditions' => array(
																					'AND' => array(
																						'JobDetail.id' => $filteredData,
																						'JobDetail.salary >=' => intval($minSalary),
																						'JobDetail.end_date >=' => date("Y-m-d")))));
					}else{
						$filteredData = $this->JobDetail->find('list', array('conditions' => array(
																					'AND' => array(
																						'JobDetail.salary >=' => intval($minSalary),
																						'JobDetail.end_date >=' => date("Y-m-d")))));
					}
					$searched = true;  
				}
			}
			if($categories != ''){
				$categoryId = $this->JobDetail->JobCategoriesJobDetail->find('list', array('conditions' => array(
																									'JobCategoriesJobDetail.job_category_id' => $categories),
																							
																							'fields' => array('JobCategoriesJobDetail.id', 'JobCategoriesJobDetail.job_detail_id')));
				if($searched){
					$filteredData = $this->JobDetail->find('list', array('conditions' => array(
																					'AND' => array(
																						'JobDetail.id' => $filteredData,
																						'JobDetail.id' => $categoryId,
																						'JobDetail.end_date >=' => date("Y-m-d")))));
				}else{
					$filteredData = $this->JobDetail->find('list', array('conditions' => array(
																				'AND' => array(
																						'JobDetail.id' => $categoryId,
																						'JobDetail.end_date >=' => date("Y-m-d")))));
				}
			}
			if($filteredData == '' || empty($filteredData)){
				$this->set('notFound', true);
			}else{
				$this->set('jobs',$this->JobDetail->find('all', array('conditions' => array(
																						'JobDetail.id' => $filteredData,
																						))));
			}
		}else{
			$this->autoRender = false;
			$jobLocations = $this->JobDetail->JobProvider->JobLocation->find('list', array('conditions' => array(
																							'JobLocation.parent_id' => NULL)));
			$jobCategories = $this->JobDetail->JobCategory->find('list', array(
																			'fields' => array(
																				'id','name'),
																			'conditions' => array(
																				'parent_id <>' => NULL )));
			$this->set('jobLocations',$jobLocations);
			$this->set('jobCategories',$jobCategories);
			$this->render('a_search');
		}
	}
	
	function required(){
			$detail['jobLocations'] = $this->JobDetail->JobProvider->JobLocation->find('list', array('conditions' => array(
																							'JobLocation.parent_id' => NULL)));
			$detail['jobCategories'] = $this->JobDetail->JobCategory->find('list', array(
																			'fields' => array(
																				'id','name'),
																			'conditions' => array(
																				'parent_id <>' => NULL )));
			return ($detail);
	}
	
	function latestJobs(){
		$this->JobDetail->recursive = 0;
		$jobs = $this->JobDetail->find('all', array('conditions' => array(
															'AND' => array(
																'JobDetail.end_date >=' => date("Y-m-d"),
																'JobDetail.published_date <=' =>  date("Y-m-d"))),
													'order' => 'JobDetail.published_date DESC',
													'limit' => 5,
													));
		return ($jobs);
	}
	
	function hotestJobs(){
		$this->JobDetail->JobDetailsStaff->recursive = 0;
		$this->JobDetail->recursive = 0;
		$jobs = $this->JobDetail->JobDetailsStaff->find('all', array(
															 'fields' => array('job_detail_id','count(job_detail_id) as number'),
															 'group' => array('job_detail_id'),
															 ));
		$i = 0;
		foreach($jobs as $value):
			$topjobs[$value['JobDetailsStaff']['job_detail_id']] = $value['0']['number'];
			$ids[$i] = $value['JobDetailsStaff']['job_detail_id'];
			$i++;
			if($i == 5)
			   break;   
		endforeach;
		
		return($this->JobDetail->find('all', array('conditions' => array('JobDetail.id' => $ids))));
	}
	
	function admin_send_mail($id = NULL, $company = NULL){
		if($id != NULL && $company != NULL){
			$email = $this->JobDetail->Staff->field('pcemail', array('Staff.id' => $id));
			$this->set(compact('email','id','company'));
		}
		if(!empty($this->data)){
			$post = '';
			$this->Email->from 		= EMAIL;
			$this->Email->sendAs 	= 'html';
			$this->Email->delivery 	= 'debug';
			$this->Email->template 	= 'feedback';
			$this->Email->to 		= $this->data['JobDetail']['email'];
			$this->Email->subject	= $this->data['JobDetail']['Subject'];
			$post = $this->data['JobDetail']['body'];
			$this->set('post', $post);
			if(!$this->Email->send()) {
				$this->Session->setFlash(__('Sending email failed. Please press the browser back button.'), 'default', array('class' => 'flashError'));
				//$this->redirect(array('controller' => 'pages' , 'action' => 'contactus'));
			}else{
				$this->Session->setFlash(__('Message Send Successful'), 'default', array('class' => 'flashError'));
				$this->redirect(array('controller' => 'job_details','action' => 'staff_view', $this->data['JobDetail']['company'], 'admin' => true));
				//$this->redirect(array('controller' => '/'));
			}
		}
	}
	
	function admin_export_application($id = NULL){
		if($id != NULL){
			$staffs = $this->JobDetail->find('all', array(
														'conditions' => array(
																'JobDetail.id' => $id)));
			$this->set('staffs',$staffs);
			$this->render('admin_export_application', 'export_application');
		}
	}
}
?>